
<%@page import="com.ptc.core.auditing.auditingResource"%>
<%@taglib prefix="w" uri="http://www.ptc.com/windchill/taglib/wrappers"%>
<%@taglib prefix="audit" uri="http://www.ptc.com/windchill/taglib/auditing"%>

<%@include file="/netmarkets/jsp/components/beginWizard.jspf"%>

<%
try{
  String displayElementId = request.getParameter("displayElementId");
  String component_id = request.getParameter("component_id");
  String pcbType = request.getParameter("pcbType");
    if(pcbType !=null && !pcbType.equals("")){
      if(pcbType.equals("FPCB_Standard")){
        pcbType = "FPCB";
      }
    }
  java.util.ArrayList availDisplayValues = ext.generic.util.NmHelper.service.getAgentList();
  
  request.setAttribute("availDisplayValues",availDisplayValues);
  request.setAttribute("availInternalValues",availDisplayValues);
%>

<u>
	<font size="5">
		Available (<%=pcbType%>) Agent List
	</font>
</u>

<br><br>

<table border="0" width="800">
<tr>
   <td width="370">
   	<span class="ppdata">Available Agent</span>
   </td>
   <td>
   	&nbsp;
   </td>
   <td width="370">
   	<span class="ppdata">Selected Agent</span>
   </td>
</tr>
<tr>
   <td rowspan="2">
      <w:comboBox id="events" name="events" multiSelect="true" size="10" displayValues="${availDisplayValues}" internalValues="${availInternalValues}"/>
   </td>
   <td align="center" valign="bottom">
      <w:button name="addEventButton" value="ADD >>" onclick="javascript:addEvents()"/>
   </td>
   <td rowspan="2">
      <w:comboBox id="selectedEvents" name="selectedEvents" multiSelect="true" size="10" displayValues="${selDisplayValues}" internalValues="${selInternalValues}"
      />
   </td>
</tr>
<tr>
   <td align="center" valign="top">
      <w:button name="removeEventButton" value="<< Remove" onclick="javascript:removeEvents()"/>
   </td>
</tr>
</table>

<br><br>
<w:button name="OKButton" value="OK" onclick="javascript:processInput()"/>
<w:button name="CancelButton" value="Cancel" onclick="javascript:window.close();"/>
<script type="text/javascript">
// Function to compare the option text for sorting purposes
function compareText(option1, option2){
   // If the spaces parameter is passed in to be sorted, it always
   // goes in the last position.
   if (option1.id == 'events_spaces' ||
       option1.id == 'selectedEvents_spaces')
   {
      return 1;
   }
   else if (option2.id == 'events_spaces' ||
            option2.id == 'selectedEvents_spaces')
   {
      return -1;
   }
   
   var first = option1.text.toLowerCase();
   var second = option2.text.toLowerCase();
   return first < second ? -1 : first > second ? 1 : 0;
}

// Function to make sure an option is not duplicated in a list
function findDup(value, list){
   value = value.toLowerCase();
   var options = list.options;
    
   for (var i = 0; i < options.length; i++)
   {
      if (options[i].value.toLowerCase() == value)
      {
         options[i].selected = true;
         return true;
      }
   }
   
   return false;
}

// Called when "Add" is selected in the UI
function addEvents(){
   var fromList = document.forms.mainform.null___events___combobox;
   var toList = document.forms.mainform.null___selectedEvents___combobox;
   moveEvents(fromList, toList);
}

// Called when "Remove" is selected in the UI
function removeEvents(){
   var toList = document.forms.mainform.null___events___combobox;
   var fromList = document.forms.mainform.null___selectedEvents___combobox;
   moveEvents(fromList, toList);
}

// Moves selected events from one list to another
function moveEvents(fromList, toList){
	var cc = fromList.length;
	var toListOptions = toList.options;
      toList.selectedIndex = -1;
	for (var i=0;i<cc;i++){
		 //alert("i="+i);
      	  var fromListOption = fromList.options[i];
      	  //alert(fromListOption.value);
   	  if (fromListOption.selected && !findDup(fromListOption.value, toList)){
      	  	
      	  //alert(fromListOption.value);
      	  var toListOption = new Option(fromListOption.text, fromListOption.value, false, true);
      	  toListOption.id = fromListOption.id;
            toListOptions[toListOptions.length] = toListOption;
      	  
      	  fromList.options[i] = null;
      	  i--;
      	  cc--;
      	}
  }
	sortSelect(toList);
}

// Sort a select element
function sortSelect(select){
   var optionArray = new Array(select.options.length);
   for (var i = 0; i < select.options.length; i++){
      optionArray[i] = new Option(
         select.options[i].text,
         select.options[i].value,
         select.options[i].defaultSelected,
         select.options[i].selected);
      optionArray[i].id = select.options[i].id;
   }
   
   optionArray.sort(compareText);
   select.options.length = 0;
   
   for (var i = 0; i < optionArray.length; i++){
      select.options[i] = optionArray[i];
   }
}

// Find an option in a particular select list
function findOption(list, value){
   var options = list.options;
   for (var i = 0; i < options.length; i++){
      if (options[i].value == value){
         return options[i];
      }
   }
}

// Run when the page loads.  It handles selecting previously selected events
function onLoad(){
	
   // Select previously selected events
   var fromList = document.forms.mainform.null___events___combobox;
   var toList = document.forms.mainform.null___selectedEvents___combobox;
   
   fromList.style.width="100%";
   toList.style.width="100%";
   
   // Select the previously selected events from the parent window.
 
   var preData = opener.window.document.getElementById("<%=component_id%>").value;

  
  var preDatas = preData.split(",");
  
   var foundOption;
   
   for (selIndex = 0; selIndex < preDatas.length; selIndex++){
      // Find option in the "from" list.
      
      foundOption = findOption(fromList, preDatas[selIndex]);
      
      // Select that option.
      if(foundOption!=null){
        foundOption.selected = true;
      }
   }

   // Move all selected options to the selected "to" list.
   addEvents();
   
   // Sort both lists.
   sortSelect(fromList);
   sortSelect(toList);
   
   return true;
}

// Called when the user presses OK.
function processInput(){
	
  var events = document.forms.mainform.null___selectedEvents___combobox;
  // Remove the spaces event
  //events.options[events.options.length - 1] = null;
  var eventOptions = events.options;
   
   // Create the comma-separated lists to pass back to the caller.
   var paramText = "";
var valueText = "";
   var returnSel = opener.window.document.getElementById("<%=displayElementId%>");
  
   var returnKeySel = opener.window.document.getElementById("<%=component_id%>");
 
   for (i = 0; i < eventOptions.length; i++){
      if (paramText != ""){
         paramText += ",";
         valueText +="\n";
      }
      valueText += eventOptions[i].value;
       paramText += eventOptions[i].value;
   }
  
   returnSel.value = valueText;
	  returnKeySel.value = paramText;
   window.close();
}




onLoad();



</script>

<%
}catch(Exception e){
  out.println(e.getLocalizedMessage());
}
%>

<%@include file="/netmarkets/jsp/util/end.jspf"%>
